package com.jqweb.services;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.jqweb.controller.Shopping.pojo.AddCartDto;
import com.jqweb.dao.CartDao;
import com.jqweb.dao.ShopDao;
import com.jqweb.entity.CartEntity;
import com.jqweb.entity.ShopEntity;
import com.jqweb.utils.Paging;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ShoppingService  {
    @Autowired
    private ShopDao shopDao;

    @Autowired
    private CartDao cartDao;

    // 分页获取商品信息
    public Map<String, Object> getAll(Paging paging) {
        // 分页查询
        IPage<ShopEntity> page = new Page<>(paging.getPage(), paging.getLimit());
        QueryWrapper<ShopEntity> queryWrapper = new QueryWrapper<>();
        IPage<ShopEntity> result = shopDao.selectPage(page,queryWrapper);
        Map<String, Object> response = new HashMap<>();
        response.put("list", result.getRecords());
        response.put("total", result.getTotal());
        return response;
    }

    // 根据id 获取商品信息
    public ShopEntity getById(Integer id) {
        return shopDao.selectById(id);
    }

    // 根据用户id查询购物车信息
    public Map<String, Object> getCartListById(Paging paging,String id){
        IPage<CartEntity> page = new Page<>(paging.getPage(), paging.getLimit());
        QueryWrapper<CartEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",id);
        IPage<CartEntity> result = cartDao.selectPage(page,queryWrapper);
        Map<String, Object> response = new HashMap<>();
        response.put("list", result.getRecords());
        response.put("total", result.getTotal());
        return response;
    }

    // 购物车添加商品
    public Integer addCart(AddCartDto data,String id){
        QueryWrapper<CartEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("goods_id",data.getGoods_id());
        CartEntity cart = cartDao.selectOne(queryWrapper);
        // 如果购物车中没有该商品，则添加
        if(cart == null){
            CartEntity cartEntity = new CartEntity();
            cartEntity.setUser_id(Integer.parseInt(id));
            cartEntity.setGoods_id(data.getGoods_id());
            cartEntity.setGoods_name(data.getGoods_name());
            cartEntity.setGoods_price(data.getGoods_price());
            cartEntity.setGoods_count(data.getGoods_count());
            cartEntity.setImg(data.getImg());
            return cartDao.insert(cartEntity);
        }
        else{
            // 如果购物车中已有该商品，则修改数量
            cart.setGoods_count(cart.getGoods_count()+data.getGoods_count());
            return cartDao.updateById(cart);
        }
    }

    // 根据id更新单个购物车数据
    public Integer updateCart(Integer id,AddCartDto data,String id1){
        CartEntity cart = cartDao.selectById(id);
        if(cart != null){
            cart.setUser_id(Integer.parseInt(id1));
            cart.setGoods_id(data.getGoods_id());
            cart.setGoods_name(data.getGoods_name());
            cart.setGoods_price(data.getGoods_price());
            cart.setImg(data.getImg());
            cart.setGoods_count(data.getGoods_count());
            return cartDao.updateById(cart);
        }
        return null;
    }
    // 根据id数组删除购物车数据(多个或者一个)
    public Integer deleteCart(List<Integer> ids){
        try{
            for (Integer id1 : ids) {
                CartEntity cart = cartDao.selectById(id1);
                if(cart != null){
                    cartDao.deleteById(cart);
                }
            }
            return 1;
        }catch (Exception e){
            return null;
        }
    }
    
    // 新增商品信息
    public Integer createShop(ShopEntity shopEntity) {
        ShopEntity shopItem= new ShopEntity();
        shopItem.setPublish_id(shopEntity.getPublish_id());
        shopItem.setGoods_name(shopEntity.getGoods_name());
        shopItem.setGoods_price(shopEntity.getGoods_price());
        shopItem.setImg(shopEntity.getImg());
        shopItem.setDescription(shopEntity.getDescription());
        shopItem.setOrigin(shopEntity.getOrigin());
        return shopDao.insert(shopItem);
    }

    // 修改商品信息
    public Integer updateShop(ShopEntity shopEntity) {
        ShopEntity shop =  shopDao.selectById(shopEntity.getId());
        if (shop == null){
            return null;
        }
        ShopEntity shopItem= new ShopEntity();
        shopItem.setPublish_id(shopEntity.getPublish_id());
        shopItem.setGoods_name(shopEntity.getGoods_name());
        shopItem.setGoods_price(shopEntity.getGoods_price());
        shopItem.setImg(shopEntity.getImg());
        shopItem.setDescription(shopEntity.getDescription());
        shopItem.setOrigin(shopEntity.getOrigin());
        return shopDao.updateById(shopEntity);
    }

    // 删除商品信息
    public Integer deleteShop(Integer id) {
        ShopEntity shop =  shopDao.selectById(id);
        if (shop == null){
            return null;
        }
        return shopDao.deleteById(id);
    }
}
